<?php
namespace app\servergoods\service\goods;

use \app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-05-25 09:55:23
 * @Description: 商品分类-后台
 */
class GoodsCategory extends ApiBase
{
    protected $goodsCategoryLogic = '\app\servergoods\logic\GoodsCategory';
    protected $goodsCategoryValidate = '\app\servergoods\validate\GoodsCategory';
    protected $addFields = ['app_id'=>'','other_app_read'=>'','is_public'=>0,'name'=>' ','cate_attrs'=>[],'pid'=>0,'profile_pic'=>'','keywords'=>'','description'=>'','order'=>100];
    protected $editFields = ['app_id'=>'','other_app_read'=>'','is_public'=>0,'name'=>' ','cate_attrs'=>[],'id'=>0,'profile_pic'=>'','keywords'=>'','description'=>'','order'=>100];

    use \app\servergoods\traits\BaseCheckCate;
    //添加或修改商品分类
    private function _updateGoodsCate(bool $isEdit)
    {
        $data = $isEdit ? $this->issetReceiveData($this->editFields) : $this->receiveData($this->addFields);

        $checkData = $this->_baseCheckCate($data, '', 'edit', $this->goodsCategoryValidate, $this->goodsCategoryLogic, 'dbOneCateInfo');
        if($checkData['status'] != SUCCESS_STATUS){
            return $checkData;
        }
        //添加或编辑差异属性
        if(isset($data['cate_attrs'])){  
            $pid = $isEdit ? $checkData['res']['pid'] : $data['pid'];
            $parentAllAttrs = $this->_parentAllAttrs(['pid'=>$pid,'app_id'=>$data['app_id']]);
            $parentAllAttrs = $parentAllAttrs['res'];

            if($parentAllAttrs){
                $commonAttrs = array_intersect($data['cate_attrs'], $parentAllAttrs);
                sort($commonAttrs);
                sort($parentAllAttrs);
                if($commonAttrs != $parentAllAttrs){
                    return verify_err(null, '子类必须拥有父类的所有属性');
                }
                $data['cate_attrs'] && $data['cate_attrs'] = implode(",", array_diff($data['cate_attrs'], $parentAllAttrs));  
            }else{
                $data['cate_attrs'] = implode(",", $data['cate_attrs']);
            }
            
        }
        if(isset($data['id'])){
            $res = $this->logic($this->goodsCategoryLogic)->editGoodsCate($data);
        }else{
            $data['level'] = $data['pid'] ? $checkData['res']['level'] + 1 : 0;
            $res = $this->logic($this->goodsCategoryLogic)->addGoodsCate($data);
        }

        return $res;
    }
    //获取该父类的所有属性合集
    public function parentAllAttrs(): array
    {
        $data = $this->receiveData(['pid'=>0, 'app_id'=>0]); 
      
        $validate = validate($this->goodsCategoryValidate);
        if(!$validate->scene('checkPid')->check($data)){
            return verify_err(null, $validate->getError());
        }

        return $this->_parentAllAttrs($data);

    }
    //获取该父类的所有属性合集
    protected function _parentAllAttrs(Array $data): array
    {
        return $this->logic($this->goodsCategoryLogic)->parentAllAttrs($data);
    }
    //添加商品分类
    public function addGoodsCate()
    {
        return $this->_updateGoodsCate(false);
    }
    //编辑商品分类
    public function editGoodsCate()
    {
        return $this->_updateGoodsCate(true);
    }
    //前台简单获取某商品分类信息
    public function frontOneCate(): array
    {
        $data = $this->issetReceiveData(['id'=>0,'app_id'=>0]);
        return $this->stepRun($data, 'frontOneCate');
    }
    //后台获取某商品分类的详细信息
    public function oneCateInfo(): array
    {
        $data = $this->issetReceiveData(['id'=>0,'app_id'=>0]);
        return $this->stepRun($data, 'oneCateInfo','oneCateInfo');
    }
    /*
        根据id数组 启用/禁用一个或多个分类
        父类被禁用，则其所有子类也被禁用，开启同理
    */
    public function changeCateStatus(): array
    {
        $data = $this->issetReceiveData(['id'=>0,'status'=>' ','app_id'=>'']);
        return $this->stepRun($data, 'changeCateStatus','changeCateStatus');
    }
    //删除一个或多个商品分类
    public function delCate(): array
    {
        $data = $this->issetReceiveData(['id'=>[],'app_id'=>0]);
        return $this->stepRun($data,'delCate','delCate');
    }
    //获取商品分类列表
    public function goodsCateList(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>0,'app_id'=>0]);
        return $this->stepRun($data,'goodsCateList','goodsCateList');
    }

    /*前台获取商品分类列表*/
    public function frontGoodsCateList(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0,'app_id'=>0,'recommend'=>'']);
        return $this->stepRun($data,'frontGoodsCateList','goodsCateList');
    }

    /*管理员推荐某个商品分类*/
    public function recommend(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0, 'recommend'=>'', 'app_id'=>0]);
        return $this->stepRun($data, 'recommend');
    }

    /*前台获取某商品父类下的推荐商品子类分类（多级）*/
    public function recommendCateList(): array 
    {
        $data = $this->haveIssetReceiveData(['pid'=>0, 'limit'=>0, 'recommend'=>' ', 'app_id'=>0]);
        return $this->stepRun($data, 'recommendCateList');
    }

    /*前台获取该商品分类下的参与搜索的商品属性列表*/
    public function getCateFilterAttr(): array 
    {
        $data = $this->haveIssetReceiveData(['app_id'=>0,'id'=>0]);
        return $this->stepRun($data, 'getCateFilterAttr');
    }

    /*前台获取该商品分类的所有分类（面包屑）*/
    public function getParentCate(): array 
    {
        $data = $this->haveIssetReceiveData(['app_id'=>0, 'id'=>0]);
        return $this->stepRun($data, 'getParentCate');
    }
}
